package com.mall.mymall.controller;

import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.EmployeeDto;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.StoresDto;
import com.mall.mymall.dao.dto.querypage.EmployeePageRequest;
import com.mall.mymall.dao.dto.querypage.StorePageRequest;
import com.mall.mymall.dao.entity.Employees;
import com.mall.mymall.service.EmployeesService;
import com.mall.mymall.service.StoresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employees")
public class EmployeesController {
    @Autowired
    private EmployeesService employeesService;

    @PostMapping("/add")
    public Result<Employees> addEmployees(@RequestBody Employees employees) {
        try{
            employeesService.save(employees);
            return Result.success(employees);
        }catch (Exception e) {
            return Result.fail("新增失败");
        }
    }

    @PutMapping(("/update/{employeeId}"))
    public Result<Employees> updateEmployees(@PathVariable("employeeId") Long employeeId, @RequestBody Employees employees) {
        try {
            // 调用服务层接口，更新员工信息
            employeesService.updateById(employees);
            // 返回成功结果，包含更新后的员工信息
            return Result.success(employees);
        } catch (Exception e) {
            return Result.fail("更新失败");
        }
    }

    @DeleteMapping("/delete/{employeeId}")
    public Result<Employees> deleteEmployees(@PathVariable("employeeId") Long employeeId) {
        try {
            // 调用服务层接口，删除员工信息
            employeesService.removeById(employeeId);
            // 返回成功结果，不返回具体数据
            return Result.success(null,"删除成功");
        } catch (Exception e) {
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/pageQuery")
    public Result<QueryDto<EmployeeDto>> pageStores(@RequestBody EmployeePageRequest employeePageRequest) {
        // 调用service层的分页查询方法，并将结果封装到Result对象中返回
        return Result.success(employeesService.pageEmployees(employeePageRequest));
    }

}
